/*
 * @lc app=leetcode.cn id=682 lang=cpp
 *
 * [682] 棒球比赛
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include<cstdlib>
#include <cmath>
#include <stack>

using namespace std;
// @lc code=start
class Solution {
public:
    int calPoints(vector<string>& ops) {
        int sum = 0;
        stack<int> ret;
        for(string s : ops) {
            if(s == "C") {
                sum -= ret.top();
                ret.pop();
            }else if(s == "D") {
                int tmp = ret.top();
                ret.push(tmp *2);
                sum += tmp * 2;
            }else if(s == "+") {
                int t1 = ret.top();
                ret.pop();
                int t2 = ret.top();
                ret.push(t1);
                ret.push(t1 + t2);
                sum += t1;
                sum += t2;
            } else {
                ret.push(stoi(s));
                sum += stoi(s);
            }
        }
        return sum;
    }
};
// @lc code=end

